package com.young.operation.config;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

class DynamicRoutingDataSource extends AbstractRoutingDataSource implements InitializingBean {

    private static final ThreadLocal<Long> CURRENT_ROUTE_KEY = new InheritableThreadLocal<>();

    @Override
    protected Object determineCurrentLookupKey() {
        return CURRENT_ROUTE_KEY.get();
    }

    public void setDataSource(Long tenantId) {
        CURRENT_ROUTE_KEY.set(tenantId);
    }

}
